/**
 * 
 */
package cz.cvut.fit.crhonjar.mi.paa.thames.solver;

import java.util.Collections;
import java.util.Comparator;

import cz.cvut.fit.crhonjar.mi.paa.thames.Configuration;
import cz.cvut.fit.crhonjar.mi.paa.thames.Item;
import cz.cvut.fit.crhonjar.mi.paa.thames.exception.ThamesException;

/**
 * @author Jarec
 * 
 */
public class LightestFirstSolver extends Solver {

	@Override
	public Configuration solve() throws ThamesException {
		Collections.sort(this.items, new Comparator<Item>() {

			@Override
			public int compare(final Item o1, final Item o2) {
				return o1.getWeight() - o2.getWeight();
			}
		});

		final Configuration conf = new Configuration(this.items);

		for (int i = 0; i < conf.getItems().size(); i++) {
			final Item item = conf.getItems().get(i);
			if ((conf.getWeight() + item.getWeight()) <= this.capacity) {
				conf.getPresence().set(i, Boolean.TRUE);
				conf.incresePrice(item.getPrice());
				conf.increaseWeight(item.getWeight());
			}
		}

		return conf;
	}

}
